'''
读取文件
'''
import configparser
import json
import os.path
import yaml

def getProjectRoot():
    #开始获取项目根目录
    current_file_path = os.path.realpath(__file__)
    current_dir_path = os.path.dirname(current_file_path)
    current_project_path = os.path.dirname(current_dir_path)
    print(current_project_path)
    return current_project_path


def read_config(option,section='env'):
    print(getProjectRoot())
    ini_file_path = getProjectRoot() + r"\test.ini"
    config = configparser.ConfigParser()
    config.read(ini_file_path)
    return config.get(section,option)

def read_yaml(file_name):
    #拼接yaml文件的路径
    file_path = getProjectRoot() + "\\test_data\\" + file_name
    #读取yaml数据文件的内容
    with open(file_path,mode='r',encoding='utf-8') as f:
        c = f.read()
    # 文件内容按照yaml格式加载到python中
    return yaml.load(c, Loader=yaml.FullLoader)


if __name__ == '__main__':
    print(read_config("base_url"))
    print(read_config("test","other"))
    print(read_config("db_info"))
    db_info = json.loads(read_config("db_info"))
    print(db_info["host"])
    y = read_yaml("register_fail_data.yaml")
    print(y)
    for y1 in y:
        print(y1["data"])